/******************** 
	作用:粉丝管理
	作者:蔡俊雄
	版本:V1.0
	时间:2015-07-25
********************/

$(function() {
	var tableId = "#dynamic-table"; //表格ID
	var replyUrl = "/Message/Reply"; //回复页面地址
	var editUrl = ""; //编辑备注的网址
	var synchronousUrl = "/Sync/FansSync"; //同步粉丝的网址
	var deleteUrl = ""; //删除的地址
	var getAppsUrl = "/app/getList"; //获取应用列表
	var superPartnerUrl = "/UserCenter/Fans/post";
	var upgradeToAgentUrl = "/agent/upgrade/update"; //升级代理的网址
	var manageUrl = "/agent/fans/update"; //管理的网址
   
	General.initCheckbox(); //初始化表格中的复选框事件监听
	General.extendDialog(); //扩展对话框
	General.initAutoPostSelect(); //初始化页面中所有自动跳转的下拉列表

	

	//-----------------管理-----------------
	//点击"管理"按钮
	$(tableId + ">tbody a[data-id=manage]:not([disabled])").on("click", function(e) {
		General.stopEvent(e);
		var tr=$(this).closest("tr");
		var id = tr.attr("data-uid");//获取粉丝id
//		var biz_id = tr.attr("data-biz_id");//获取biz_id
//		var public_id = tr.attr("data-public_id");//获取public_id
		var title="管理";
		showManageDialog(title,id);
	});
	
	//显示"管理"窗口
	function showManageDialog(title,id){
		var url = Common.addUrlParameters(manageUrl, {"uid": id});
		var maxWidth=700;
		var maxHeight=580;
		var width = $(window).width(); //获取窗口的宽度
		width = width * 0.9;
		if(width>maxHeight){
			width=maxWidth;
		}else{
			width="90%";
		}
		
		var height = $(window).height(); //获取窗口的高度
		height = height * 0.9;
		if(height>maxHeight){
			height=maxHeight;
		}else{
			height=0;
		}
		General.showDialogWidthHeight(title,url,width,height);
	}

	//-----------------升级代理-----------------
	//点击"升级代理"按钮
	$(tableId + ">tbody a[data-id=agent]:not([disabled])").on("click", function(e) {
		General.stopEvent(e);
		var id = $(this).closest("tr").attr("data-uid");//获取粉丝id
		var title="升级代理";
		showAgentDialog(title,id);
	});
	
	//显示"升级代理"窗口
	function showAgentDialog(title,id){
		var url = Common.addUrlParameters(upgradeToAgentUrl, {"id": id});
		var height = $(window).height(); //获取窗口的高度
		height = height * 0.9;
		var maxHeight=670;
		if(height>maxHeight){
			height=maxHeight;
		}else{
			height=0;
		}
		General.showDialogWidthHeight(title,url,500,height);
	}
	//---------------------超级合伙人
	$('.super_partner').on("click",function(){		
		var tips = $(this).text();
		var is_subscribe = $(this).attr("data-subscribe");
		if(is_subscribe == 0) {
			alert("你是游客，不能成为超级合伙人");
			return;
		}
		if (confirm("您确定"+tips+'?')) {
			var is_super_partner=$(this).attr("data-super");
			var uid = $(this).closest("tr").attr("data-uid");
			superPartner(is_super_partner,uid); 
		}
	});
	
	function superPartner(is_super_partner,uid) {
		var options = {};
		var url = superPartnerUrl;
				
		var data = {
				"is_super_partner":is_super_partner,
				"uid":uid,
				'update': 'update'
		};
		var tip = "正在设置";
		Common.ajaxPost(options, berforSuperPartner, url, data, tip, completeSuperPartner, failSuperPartner);
	};
	function berforSuperPartner(){}
	
	function completeSuperPartner(options, data, status) {
			data=$.parseJSON(data);
			if(data.success){
				alert("设置成功!");
			}
		General.hideMask();
		General.refresh(); //刷新页面
	};
	
	function failSuperPartner() {	
		alert("设置失败!");
	};
	
	//---------------------成为分销客
	$('.sharer').on("click",function(){		
		var tips = $(this).text();
		if (confirm("您确定"+tips+'?')) {
			var is_sharer=$(this).attr("data-super");
			var uid = $(this).closest("tr").attr("data-uid");
			sharer(is_sharer,uid); 
		}
	});
	
	function sharer(is_sharer,uid) {
		var options = {};
		var url = superPartnerUrl;
				
		var data = {
				"is_sharer":is_sharer,
				"uid":uid,
				'update': 'update'
		};
		var tip = "正在设置";
		Common.ajaxPost(options, berforSharer, url, data, tip, completeSharer, failSharer);
	};
	function berforSharer(){}
	
	function completeSharer(options, data, status) {
			data=$.parseJSON(data);
			if(data.success){
				alert("设置成功!");
			} else {
				alert(data.msg);
			}
		General.hideMask();
		General.refresh(); //刷新页面
	};
	
	function failSharer() {	
		alert("设置失败!");
	};
	
	
	//---------------------------同步粉丝
	//点击"同步粉丝"按钮
	$("#synchronous").on("click", function(e) {
	    if(confirm("确定 [同步粉丝] 操作")){	    	
	    	synchronousFans();
	    }
		
	});
	/**
	 * 同步粉丝
	 */
	function synchronousFans() {
		var options = {};
		var url = synchronousUrl;
		var data = {};
		var tip = "正在同步粉丝.....";
		var method = 'post';
		Common.ajaxPost(options, beforeSynchronousFans, url, data, tip, completeSynchronousFans, failSynchronousFans,method);
	};
	//同步粉丝前的操作
	function beforeSynchronousFans() {
		//General.showMask();
		 
	};
	//同步粉丝成功
	function completeSynchronousFans(options, data, status) {
		
		data=$.parseJSON(data);
		if(data.success){
			alert('同步完成,总共耗时['+data.cost_time+']s');
			General.hideMask();
			General.refresh(); //刷新页面
		}else{
			
			failSynchronousFans(data);
		}
		
	};
	//同步粉丝失败
	function failSynchronousFans(data) {	
		General.showMask();
		alert("同步粉丝失败["+data.msg+"]");
		General.hideMask();
	};
	
	//-----------------添加到分组-----------------
	//点击"添加到分组"按钮
	/*$("#sureAdd").on("click", function(e) {
		//判断是否有行被选中
		var selectedRows = $(tableId+' > tbody input[type=checkbox]:checked');
		if (selectedRows.length > 0) {
			//判断是否选中了分组
			if($("#addToGroup").val()=="0"){
				General.alert("请先选中要添加到的分组");
				return false;
			}
			return true;
		} else {
			General.alert("请先选中要添加到分组的粉丝再进行操作!");
			return false;
		}
	});*/
	
	//-----------------发送消息-----------------
	//点击"发送消息"按钮
	$(tableId + ">tbody a[data-id=reply]:not([disabled])").on("click", function(e) {
		General.stopEvent(e);
		var id = $(this).closest("tr").attr("data-id");//获取粉丝id
		var title="快速回复";
		showReplyDialog(title,id);
	});
	
	/**
	 * 显示"发送消息"窗口
	 * @param {String} title 弹出窗口的标题
	 * @param {String} id 粉丝id
	 */
	function showReplyDialog(title,id){
		var url = Common.addUrlParameters(replyUrl, {"id": id});
//		General.showDialog(title,url);
		General.showDialogWidthHeight(title,url,500,420);
//		General.showDialogWidthHeight(title,url,500,340);
	}
	
	//---------------------------编辑备注
	//点击行内"备注"按钮
	$(tableId + ">tbody a[data-id=edit]:not([disabled])").on("click", function(e) {
		$("#dialog-edit").html($("#tpl-remark").html());
		var tr=$(this).closest("tr");
		var id = tr.attr("data-id"); //获取id
		var text = tr.find("td:eq(2) span:eq(1)").text();
		$("#dialog-edit input[type=text]").val(text); //设置备注
		$("#dialog-edit").removeClass('hide').dialog({
			resizable: false,
			width: '360',
			modal: true,
			title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-edit green'></i> 编辑备注</h4></div>",
			title_html: true,
			buttons: [{
				html: "<i class='ace-icon fa fa-save'></i> 确定",
				"class": "btn btn-info btn-minier btn-space-right",
				click: function() {
					var result = $("#dialog-edit input[type=text]").val(); //获取备注名称
//					if (!result) {
//						alert("请输入备注!");
//						$("#dialog-edit input[type=text]").focus();
//						return;
//					}
					var options = {};
					var url = editUrl;
					var data = {
						"id": id,
						"name": result
					};
					var tip = "正在编辑备注";
					Common.ajaxPost(options, beforeEdit, url, data, tip, completeEdit, failEdit);
				}
			}, {
				html: "<i class='ace-icon fa fa-times'></i> 取消",
				"class": "btn btn-danger btn-minier",
				click: function() {
					$(this).dialog("close");
				}
			}]
		});
	});
	//编辑备注前的操作
	function beforeEdit() {
		General.closeEditWindow();
		General.showMask();
	};
	//编辑备注成功
	function completeEdit(options, data, status) {
		//General.hideMask();
		//		data=$.parseJSON(data);
		//		if(data.success){
		//			alert("编辑备注成功!");
		//		}
		General.hideMask();
		General.refresh(); //刷新页面
	};
	//编辑备注失败
	function failEdit() {
		General.hideMask();
		alert("编辑备注失败!");
	};
	//-----------------删除-----------------
	/*//点击删除所选按钮
	$("#delete").on("click", function(e) {
		General.stopEvent(e);
		//判断是否有行被选中
		var selectedRows = $(tableId+' > tbody input[type=checkbox]:checked');
		if (selectedRows.length > 0) {
			//弹出确认框
			if (confirm("您确定要删除所有选中的粉丝吗?")) {
				var ids = [];
				var id;
				selectedRows.each(function(index, checkbox) {
					id = $(this).closest("tr").attr("data-id");
					ids.push(id);
				});
				startDelete(ids); //开始删除
			}
		} else {
			General.alert("请先选中要删除的粉丝再进行操作!");
		}
	});
	//点击行内"删除"按钮
	$(tableId + ">tbody a[data-id=delete]:not([disabled])").on("click", function(e) {
		var tr = $(this).closest("tr");
		var id = tr.attr("data-id");
		var text = tr.find("td:eq(1)").text();
		if (confirm("您确定要删除粉丝\"" + text + "\"吗?")) {
			//		if (confirm("您确定要删除该分组吗?")) {
			startDelete([id]); //开始删除消息
		}
	});
	//开始删除
	function startDelete(ids) {
		General.showMask();
		var options = {
			"id": ids
		};
		var url = deleteUrl;
		var data = {
			"id": ids.join(",")
		};
		var tip = "正在删除";
		Common.ajaxPost(options, null, url, data, tip, completeDelete, failDelete);
	};
	//删除成功
	function completeDelete(options, data, status) {
		General.hideMask();
		window.location.reload();
	};
	//删除失败
	function failDelete() {
		General.hideMask();
		alert("删除失败!");
	};*/

	//-----------------模拟登陆-----------------
	//点击"模拟登陆"按钮
	$(tableId + ">tbody a[data-id=simulateLogin]:not([disabled])").on("click", function(e) {
		General.stopEvent(e);
		var uid = $(this).closest("tr").attr("data-authid");//获取粉丝id
		var title="选择应用";
		showSimulateLoginDialog(title,uid);
	});
	
	//显示"模拟登陆"窗口
	function showSimulateLoginDialog(title,uid){
		var url = Common.addUrlParameters(getAppsUrl, {"uid": uid});
		var height = $(window).height(); //获取窗口的高度
		General.showDialogWidthHeight(title,url,500,height);
	}
});
